#!/bin/bash
#
#
# ``plugin.sh`` calls the following methods in the sourced driver:
#
# - nb_db_driver_install_server
# - nb_db_driver_install_client
# - nb_db_driver_start_server
# - nb_db_driver_stop_server
# - nb_db_driver_clean
# - nb_db_driver_configure

RAMCLOUD=$DEST/ramcloud
RAMCLOUD_LIB=$RAMCLOUD/lib
RAMCLOUD_BIN=$RAMCLOUD/bin
RAMCLOUD_MASTER_IP=${RAMCLOUD_MASTER_IP:-"$HOST_IP"}
RAMCLOUD_COORDINATOR_IP=${RAMCLOUD_COORDINATOR_IP:-"$HOST_IP"}
RAMCLOUD_MASTER_PORT=${RAMCLOUD_MASTER_PORT:-'21221'}
RAMCLOUD_COORDINATOR_PORT=${RAMCLOUD_COORDINATOR_PORT:-'21222'}
RAMCLOUD_TRANSPORT=${RAMCLOUD_TRANSPORT:-'fast+udp'}

LIB_BOOST_MAJOR_VERSION=1
LIB_BOOST_MINOR_VERSION=54

function nb_db_driver_install_server {
    if is_service_enabled df-rcmaster ; then
        echo "Installing Dependencies"
        if is_ubuntu; then
            boost_program=libboost-program-options"$LIB_BOOST_MAJOR_VERSION"."$LIB_BOOST_MINOR_VERSION"
            boost_filesystem=libboost-program-options"$LIB_BOOST_MAJOR_VERSION"."$LIB_BOOST_MINOR_VERSION"
            protobuf_lib=libprotobuf8
        elif is_suse || is_oraclelinux; then
            boost_program=libboost_program_options"$LIB_BOOST_MAJOR_VERSION"_"$LIB_BOOST_MINOR_VERSION"_0
            boost_filesystem=libboost_filesystem"$LIB_BOOST_MAJOR_VERSION"_"$LIB_BOOST_MINOR_VERSION"_0
            protobuf_lib=libprotobuf8
        elif is_fedora; then
            if [[ "$os_RELEASE" -ge "21" ]]; then
                echo "Boost version 54 is not available for fedora > 20"
                #TODO(gampel) add support for fedora > 20
            else
                boost_program=boost_program_options"$LIB_BOOST_MAJOR_VERSION"_"$LIB_BOOST_MINOR_VERSION"_0
                boost_filesystem=boost_filesystem"$LIB_BOOST_MAJOR_VERSION"_"$LIB_BOOST_MINOR_VERSION"_0
            fi
            protobuf_lib=protobuf
        fi
        install_package "$boost_program" "$boost_filesystem" "$protobuf_lib"
        echo "Installing RAMCloud server"
        git_clone https://github.com/dsivov/RamCloudBin.git $RAMCLOUD
        echo export LD_LIBRARY_PATH="$RAMCLOUD_LIB":"$LD_LIBRARY_PATH" | tee -a $HOME/.bashrc
    fi
}

function nb_db_driver_install_client {
    echo "Installing RAMCloud client"
    git_clone https://github.com/dsivov/RamCloudBin.git $RAMCLOUD
}

function nb_db_driver_start_server {
    if is_service_enabled df-rccoordinator ; then
        $RAMCLOUD_BIN/coordinator -C ${RAMCLOUD_TRANSPORT}:host=${RAMCLOUD_COORDINATOR_IP},port=${RAMCLOUD_COORDINATOR_PORT} 2&> /dev/null || true &
    fi
    if is_service_enabled df-rcmaster ; then
        sleep 10
        $RAMCLOUD_BIN/server -L ${RAMCLOUD_TRANSPORT}:host=${RAMCLOUD_MASTER_IP},port=${RAMCLOUD_MASTER_PORT} -C ${RAMCLOUD_TRANSPORT}:host=${RAMCLOUD_COORDINATOR_IP},port=${RAMCLOUD_COORDINATOR_PORT} 2&> /dev/null || true &
        echo "Sleep for 20 secs. Giving time for db to start working!!!"
        sleep 20
    fi
}

function nb_db_driver_stop_server {
    if is_service_enabled df-rccoordinator ; then
        sudo killall coordinator 2&> /dev/null || true
    fi
    if is_service_enabled df-rcmaster ; then
        sudo killall server 2&> /dev/null || true
    fi
}

function nb_db_driver_clean {
    sudo rm -rf $RAMCLOUD
}

function nb_db_driver_configure {
    :
}
